import pandas as pd

# 内连接
df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value': [1, 2, 3, 4]})
df2 = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value': [5, 6, 7, 8]})
inner_join = pd.merge(df1, df2, on='key', how='inner')
print("内连接结果：\n", inner_join)

# 自然连接
df1 = pd.DataFrame({'key': ['A', 'B', 'C'], 'value': [1, 2, 3]})
df2 = pd.DataFrame({'key': ['B', 'D', 'E'], 'value': [4, 5, 6]})
natural_join = pd.merge(df1, df2, on='key', how='outer')
print("\n自然连接结果：\n", natural_join)

# 左半连接
df1 = pd.DataFrame({'key': ['A', 'B', 'C'], 'value': [1, 2, 3]})
df2 = pd.DataFrame({'key': ['B', 'D', 'E'], 'value': [4, 5, 6]})
left_join = pd.merge(df1, df2, on='key', how='left')
print("\n左半连接结果：\n", left_join)

# 右半连接
df1 = pd.DataFrame({'key': ['A', 'B', 'C'], 'value': [1, 2, 3]})
df2 = pd.DataFrame({'key': ['B', 'D', 'E'], 'value': [4, 5, 6]})
right_join = pd.merge(df1, df2, on='key', how='right')
print("\n右半连接结果：\n", right_join)

# 反连接
df1 = pd.DataFrame({'key': ['A', 'B', 'C'], 'value': [1, 2, 3]})
df2 = pd.DataFrame({'key': ['B', 'D', 'E'], 'value': [4, 5, 6]})
anti_join = pd.concat([df1, df2]).drop_duplicates(keep=False)
print("\n反连接结果：\n", anti_join)
